package cn.itsource.blog.controller;

import cn.itsource.blog.domain.ArticleType;
import cn.itsource.blog.query.ArticleTypeQuery;
import cn.itsource.blog.service.IArticleTypeService;
import cn.itsource.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：文章类型ArticleType
 */
@RestController
@RequestMapping("/articleType")
@Api(tags = "文章类型接口类")
public class ArticleTypeController {
    @Autowired
    private IArticleTypeService articleTypeService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<ArticleType> findAll(){
        return articleTypeService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ArticleType findOne(@PathVariable("id")Long id){
        return articleTypeService.findOne(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
            articleTypeService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * @param articleType
     * @return
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody ArticleType articleType){
        try {
           if(articleType.getId()==null){
               articleTypeService.add(articleType);
           }else{
               articleTypeService.update(articleType);
           }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody Long[] ids){
        try {
            articleTypeService.patchDel(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * @param articleTypeQuery
     * @return
     */
    @PostMapping
    public PageInfo<ArticleType> queryPage(@RequestBody ArticleTypeQuery articleTypeQuery){
        return articleTypeService.queryPage(articleTypeQuery);
    }

    @ApiOperation(value = "查询类型树")
    @GetMapping("/typeTree")
    public List<ArticleType> typeTree(){
        return articleTypeService.typeTree();
    }
}
